package com.zhugang.week04;

/**
 * @program algorithms
 * @description: myPow
 * @author: chanzhugang
 * @create: 2022/06/24 11:45
 */
public class MyPow2 {

    public static void main(String[] args) {
        MyPow2 myPow2 = new MyPow2();
        double res = myPow2.myPow(2, 8);
        System.out.println(res);
    }

    /**
     * 数值的整数次方
     *
     * @param x
     * @param n
     * @return
     */
    public double myPow(double x, int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        if (n == -1) {
            return 1 / x;
        }
        double half = myPow(x, n / 2);
        double mod = myPow(x, n % 2);
        return half * half * mod;
    }
}